package com.eng.carro.mb;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.eng.carro.entities.DeliveryPlace;
import com.eng.carro.services.DeliveryPlaceService;

@ManagedBean( name = "placeMB" )
@SessionScoped
public class PlaceMBean implements Serializable {
  
  private static final long serialVersionUID = 2141044184132153420L;
  
  private DeliveryPlace place;
  private DeliveryPlace selected;
  
  @EJB DeliveryPlaceService service; 
  
  private List<DeliveryPlace> values;
  
  @PostConstruct
  public void init() {
	place = new DeliveryPlace();
  }

  public List<DeliveryPlace> getValues() {
	if ( values == null ) {
	  return values = service.findAll();	  
	}
	
	return values;
  }

  public DeliveryPlace getPlace() {
    return place;
  }

  public void setPlace(DeliveryPlace place) {	
    this.place = place;
  }
  
  public void setValues(List<DeliveryPlace> values) {
    this.values = values;
  }

  public void insert() {
	if ( place != null && place.getLocal().isEmpty() ) {
	  FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
			"Informe o Local", "Erro");
	  FacesContext.getCurrentInstance().addMessage(null, message);
	  return;
	}
	
	service.makePersistent( place );
	
	FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
		"Registro inserido com sucesso!", "Sucesso");
	FacesContext.getCurrentInstance().addMessage(null, message);
	
	setValues( service.findAll() );
	setPlace( new DeliveryPlace() );
  }
  
  public void delete( DeliveryPlace place ) {
	service.delete(place);
  }
  
  public DeliveryPlace getSelected() {
    return selected;
  }

  public void setSelected(DeliveryPlace selected) {
    this.selected = selected;
  }
  
  public void onRowSelected() {
	
  }
}
